<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>页面</title>
    <script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/loading.js"></script>
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/theme-chalk/index.css" rel="stylesheet">

</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">基础库</el-breadcrumb-item>
                <el-breadcrumb-item>单位库</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <el-header>

            <el-row >
                <!--<a href="/Users/10659/Desktop/新建文件夹%20(4)/user/userAdd.html">-->
                <#--<el-col :xs="8" :sm="8" :md="4" :lg="2" :xl="2">-->
                    <#--<el-button type="primary" icon="el-icon-edit" :offset="6" @click="add">新增</el-button>-->
                <#--</el-col>-->
                <!--</a>-->
                <el-col :xs="14" :sm="14" :md="8" :lg="8" :xl="8">
                    <el-input placeholder="请输入内容" @keyup.enter.native="enterSearch" v-model="keySearch">
                        <el-button slot="append" icon="el-icon-search" @click=search></el-button>
                    </el-input>
                </el-col>
            </el-row>
        </el-header>
        <el-main>
            <el-table :data="tableData" stripe border style="width:100%" highlight-current-row size="mini" tooltip-effect="dark" height="calc(100vh - 240px)">
                　　
                <el-table-column label="序号" align="center" min-width="50">
                    　　<template scope="scope"><span>{{scope.$index+(currentPage - 1) * pageSize + 1}}</span></template>
                </el-table-column>
                　　
                <el-table-column prop="unit_name" label="名称" align="center" min-width="100">
                    　　
                </el-table-column>
                　　
                <el-table-column prop="unit_status" label="状态" align="center" min-width="100" :formatter="formatStatus"
                                 >
                    　　
                </el-table-column>
                　　
                <el-table-column label="操作" align="center" fixed="right" min-width="100">
                    　　　　
                    <template slot-scope="scope">
                        <#--<el-button type="text" v-if="scope.row.unit_status==00"-->
                                   <#--@click="modify(scope.row.id,scope.row.unit_name)">编辑-->
                        <#--</el-button>-->
                        <el-button type="text" v-if="scope.row.unit_status==00" @click="remove(scope.row.id,scope.row.unit_name)">作废</el-button>
                    </template>
                    　　
                </el-table-column>

            </el-table>
        </el-main>
        <el-footer>
            <el-row >
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                               :page-sizes="[100, 500, 1000]" :page-size="pageSize"
                               layout="total, sizes, prev, pager, next, jumper"
                               :total="totalRecords" >
                </el-pagination>
            </el-row>
        </el-footer>
        <el-dialog title="请输入单位信息" :visible.sync="dialogFormVisible" @close="close">
            <el-form label-width="20%">
                <el-row>
                    <el-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                        <el-form-item label="单位名称" prop="unitName">
                            <el-input v-model="unitName"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="submit()">确 定</el-button>
            </div>
        </el-dialog>
    </el-container>
</div>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/dist/vue.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/index.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/publiccss/assets/global/plugins/jquery.min.js" type="text/javascript"></script>

<script >
    new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],
                currentPage: 1, //当前页
                pageSize: 100, //每页总数
                totalRecords: 0, //总条数
                keySearch: '',
                dialogFormVisible: false,
                id: '',
                unitName: '',
            };
            show: false

        },

        methods: {
            close(){
                this.unitName = '';
                this.id = '';
            },
            //打开新增弹窗
            add() {
                this.dialogFormVisible = true;
            },
            //状态转换
            formatStatus: function (row, column) {
                return row.unit_status === '00' ? '有效' : row.unit_status === '99' ? '无效' : '未知';
            },
            //回车监听事件获取输入内容
            enterSearch(event) {
                this.currentPage=1;
                this.pageSize=100;
                this.keySearch = event.target.value;
                this.ajaxFunction()
            },
            //点击事件获取输入内容
            search() {
                this.currentPage=1;
                this.pageSize=100;
                this.ajaxFunction()
            },
            //作废
            remove(id,unit_name) {
                this.$confirm('此操作将永久作废该单位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    $.ajax({
                        url: '/dataCenter/unitEdit.json',
                        type: 'post',
                        data: {
                            id:id,
                            unit_name:unit_name,
                            unit_status:99,
                            type:"cancel"
                        },
                        dataType: 'json',
                        success: (result)=> {
                            if (result.ret) {
                                this.ajaxFunction();
                                this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });

                            }else{
                                this.$message.error('操作失败')
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            //打开修改弹窗
            modify(id, unit_name) {
                this.unitName = unit_name;
                this.id = id;
                this.dialogFormVisible = true;
                console.log(id, unit_name);
            },
            //取消
            cancel() {
                this.unitName = '';
                this.id = '';
                this.dialogFormVisible = false;
            },
            //提交
            submit() {
                var boolean = false;
                var msg = '';
                if (''=== this.id) {
                    $.ajax({
                        url: '/dataCenter/unitEdit.json',
                        type: 'post',
                        data: {
                            brand_name:this.unitName,
                            type:"add"
                        },
                        dataType: 'json',
                        success: (result) =>{
                            boolean = result.ret;
                            if(!boolean){
                                msg = result.msg;
                            }
                        }
                    });
                } else {
                    $.ajax({
                        url: '/dataCenter/unitEdit.json',
                        type: 'post',
                        data: {
                            id:this.id,
                            unit_name:this.unitName,
                            type:"update"
                        },
                        dataType: 'json',
                        success: (result)=> {
                            boolean = result.ret;
                            if(!boolean){
                                msg = result.msg;
                            }
                        }
                    });
                }
                if (boolean) {
                    this.dialogFormVisible = false;
                    this.unitName = '';
                    this.id = '';
                    this.ajaxFunction()
                }else{
                    this.$message.error(msg);
                }
            },
            async handleSizeChange(val) {
                this.pageSize = val;
                this.ajaxFunction()
            },
            //
            async handleCurrentChange(val) {

                this.currentPage = val;
                this.ajaxFunction()
            },
            ajaxFunction() {
                $.ajax({
                    url: '/dataCenter/unitList.json?param='+this.keySearch+'&page='+this.currentPage+'&limit='+this.pageSize,
                    type: 'get',
                    dataType: 'json',
                    success: (result) =>{
                        if (result.ret) {
                            this.tableData = result.data;
                            this.totalRecords = result.count;
                        }else{
                            this.$message.error('列表加载失败，请稍后重试');
                        }
                    }
                });
            },
        },
        mounted() {
            this.ajaxFunction();
        },
    })
</script>

</body>
</html>

